import { addSort, getSort, updateSort, delSort } from 'api/sort'

const sort = {
  state: {
    sortList: [],
    sortTotal: 0
  },
  mutations: {
    SET_SORTLIST(state, data) {
      state.sortList = data.list
      state.sortTotal = data.total
    }
  },
  actions: {
    GetSortList({ commit }, params) {
      return new Promise((resolve, reject) => {
        getSort(params).then(response => {
          commit('SET_SORTLIST', response.data)
          resolve(response.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    AddSort({ commit }, params) {
      return new Promise((resolve, reject) => {
        addSort(params).then(response => {
          resolve(response)
        }).catch(err => {
          reject(err)
        })
      })
    },
    UpdateSort({ commit }, params) {
      const id = params.id
      delete params.id
      return new Promise((resolve, reject) => {
        updateSort(id, params).then(response => {
          resolve(response)
        }).catch(err => {
          reject(err)
        })
      })
    },
    DelSort({ commit }, id) {
      return new Promise((resolve, reject) => {
        delSort(id).then(response => {
          resolve(response)
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}
export default sort
